package commEntity

import (
	"encoding/json"
)

type UserEntity struct {
	BaseEntity *BaseEntity // 基本信息

	UserName   string // 设备厂商名称
	PassWord   string // 设备类型名
	UserType   string // 设备名称
	Role       string // 通道类型
	Permission string // 通道名称
	Menu       string // 配置参数
}

type userPo struct {
	Id         int64  `json:"id" db:"id"`                  // 对象id
	CreateTime int64  `json:"createTime" db:"create_time"` // 创建时间
	UpdateTime int64  `json:"updateTime" db:"update_time"` // 更新时间
	UserName   string `json:"username" db:"username"`      // 设备厂商名称
	PassWord   string `json:"password" db:"password"`      // 设备类型名
	UserType   string `json:"userType" db:"user_type"`     // 设备名称
	Role       string `json:"role" db:"role"`              // 通道类型
	Permission string `json:"permission" db:"permission"`  // 通道名称
	Menu       string `json:"menu" db:"menu"`              // 配置参数
}

func (e *UserEntity) Type() string {
	return "UserEntity"
}

func (e *UserEntity) TableName() string {
	return "tb_user"
}

func (e *UserEntity) GetBaseEntity() *BaseEntity {
	return e.BaseEntity
}

func (e *UserEntity) Instance() *UserEntity {
	return &UserEntity{
		BaseEntity: new(BaseEntity),
	}
}

func (e *UserEntity) NewEntity() IEntity {
	return e.Instance()
}

func (e *UserEntity) NewPo() interface{} {
	return &userPo{}
}

func (e *UserEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return MakeServiceKey(p)
}

func (e *UserEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.UserName)
	return p
}

func (e *UserEntity) BuildByJson(jsn *string) error {
	var po userPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *UserEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *UserEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*userPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.Menu = po.Menu
	ptr.UserName = po.UserName
	ptr.PassWord = po.PassWord
	ptr.UserType = po.UserType
	ptr.Role = po.Role
	ptr.Permission = po.Permission
}

func (e *UserEntity) BuildToPo() interface{} {
	ptr := e
	po := userPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.Menu = ptr.Menu
	po.UserName = ptr.UserName
	po.PassWord = ptr.PassWord
	po.UserType = ptr.UserType
	po.Role = ptr.Role
	po.Permission = ptr.Permission

	return &po
}
